//
//  DiseaseInfo_viewController.m
//  International_Hospital
//
//  Created by 李建强 on 16/7/6.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "DiseaseInfo_viewController.h"
#import "DiseaserTopView.h"
#import "DesCell.h"
#import "DesModel.h"



@interface DiseaseInfo_viewController ()<UITableViewDelegate,UITableViewDataSource,DiseasearTopDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,DiseaClickDelegate>

@property (nonatomic,strong)UIScrollView *scrollView;



@property (nonatomic,strong)UITableView *tableview;

@property (nonatomic,strong)UIImageView *picImageView;

@property (nonatomic,strong)IH_Label *nameLabel;

@property (nonatomic,strong)IH_Label *profLabel;

@property (nonatomic,strong)IH_Label *hospitalLabel;


@property (nonatomic,strong)DiseaserTopView *topView;

@property (nonatomic,strong)NSArray<DesModel*> *dataArray;

@property (nonatomic,strong)UIBarButtonItem*rightItem;

@property (nonatomic,assign)BOOL EditOrOff;

//@property (nonatomic,strong)DesCell*cell;
@property (nonatomic,strong)NSMutableDictionary *muDic;

@property (nonatomic,strong)UIButton *picButton;

@property (nonatomic,strong)UIImageView *bgImageView;

@property (nonatomic,strong)UIImagePickerController *imagePickerVc;

@property (nonatomic,assign)BOOL open;

@end

@implementation DiseaseInfo_viewController

-(NSArray<DesModel *> *)dataArray{
    
    if (_dataArray == nil) {
        
        DesModel*model = [[DesModel alloc]init];
        _dataArray = [model desDataArray];
        
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置顶部右侧编辑按钮
    [self setHeadItem];
    [self setupUI];
    [self setupLayout];
}



-(void)setHeadItem{
    
    NSString*title = @"编辑";
//    if (self.EditOrOff) {
//        title = @"确定";
//    }else{
//        
//        title = @"编辑";
//    }
    
    self.rightItem = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(didClickEdit)];
    self.rightItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = self.rightItem;
    
}


-(void)didClickEdit{
    
    NSLog(@"--");
    
    
    NSIndexPath *indexPath;
    DesCell *cell;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    
    self.muDic = [NSMutableDictionary dictionary];
    
    if (self.EditOrOff) {
        self.rightItem.title = @"编辑";
        for (int i = 0; i< self.dataArray.count; i++) {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            cell = [self.tableview cellForRowAtIndexPath:indexPath];
            
            if (i == 0) {
                
                if (self.disBlock) {
                    self.disBlock(cell.tx_Label.text);
                }
            }
            
            [self.muDic setObject:cell.tx_Label.text forKey:cell.titleLabel.text];
            
            [defaults setObject:cell.tx_Label.text forKey:cell.titleLabel.text];
            [defaults synchronize];
        }
        
        if (self.dicBlock) {
            self.dicBlock(self.muDic.copy);
        }
        self.EditOrOff = NO;
        [self.tableview reloadData];
        
        
//        [self.navigationController popViewControllerAnimated:YES];
    }else{
        self.rightItem.title = @"确定";
        for (int i = 0; i< self.dataArray.count; i++) {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            cell = [self.tableview cellForRowAtIndexPath:indexPath];
            cell.tx_Label.enabled = YES;
        }
        self.EditOrOff = YES;
    }
    
}

-(void)setupUI{
    
    self.scrollView = [[UIScrollView alloc]init];
    
    
    
    [self.view addSubview:self.scrollView];
    
    
    
    
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource =self;
    [self.view addSubview:self.tableview];
    
    self.bgImageView = [[UIImageView alloc]init];
    self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    NSFileManager *manger = [NSFileManager defaultManager];
    if ([manger fileExistsAtPath:BIGPATH]) {
        self.bgImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:BIGPATH]];
    }else{
        
        self.bgImageView = [[UIImageView alloc]init];
    }
    
    
    
    
    self.tableview.backgroundView = self.bgImageView;
    
    //注册
    [self.tableview registerClass:[DiseaserTopView class] forHeaderFooterViewReuseIdentifier:@"head"];
    [self.tableview registerClass:[DesCell class] forCellReuseIdentifier:@"desCell"];
    
    self.tableview.tableFooterView = [[UIView alloc]init];
    
    
    
}





/*****************左侧缩进为0*********************/
- (void)viewDidLayoutSubviews
{
    
    [super viewDidLayoutSubviews];
    self.bgImageView.frame = self.tableview.bounds;
//    [NSThread sleepForTimeInterval:3];
//    NSLog(@"%@",self.picImageView);
    
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableview setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
/*****************左侧缩进为0*********************/


#pragma 取消选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [cell.tx_Label becomeFirstResponder];
    [self.tableview deselectRowAtIndexPath:indexPath animated:YES];
}



#pragma 约束设置
-(void)setupLayout{
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.scrollView).offset(65);
        make.left.equalTo(self.scrollView);
        make.right.equalTo(self.scrollView);
        make.bottom.equalTo(self.scrollView);
    }];

}

#pragma 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desCell" forIndexPath:indexPath];
    DesModel*model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.4];
    cell.dataDic = self.muDic;
    cell.tx_Label.enabled= NO;
    
   
    
    return cell;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    self.topView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    self.topView.delegate =self;
    self.topView.clickDelegate =self;
    return self.topView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 100;
}


-(void)diseasearTop:(DiseaserTopView *)diseaser addPicButton:(UIButton *)button{
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    //资源类型为图片库
    
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     picker.delegate = self;
     picker.allowsEditing = YES;
    
    self.open = YES;
    self.picButton = button;
    
     [self presentViewController:picker animated:YES completion:nil];
  
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    //当图片不为空时显示图片并保存图片
    
    if (image != nil) {
        
        //图片显示在界面上
        
        //        [changeImg setBackgroundImage:image forState:UIControlStateNormal];
        
        
        //以下是保存文件到沙盒路径下
        
        //把图片转成NSData类型的数据来保存文件
        
        NSData *data;
        
        //判断图片是不是png格式的文件
        
        if (UIImagePNGRepresentation(image)) {
            
            //返回为png图像。
            
            data = UIImagePNGRepresentation(image);
            
        }else {
            
            //返回为JPEG图像。
            
            data = UIImageJPEGRepresentation(image, 1.0);
            
        }
        
        if (self.open) {
            [[NSFileManager defaultManager] createFileAtPath:PATH contents:data attributes:nil];
        }else{
            
            [[NSFileManager defaultManager] createFileAtPath:BIGPATH contents:data attributes:nil];
        }
        
        
        //保存
        
        
        
    }
    
    //关闭相册界面
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        if (self.open) {
            [self.picButton setImage:image forState:UIControlStateNormal];
            if (self.picBlock) {
                self.picBlock(self.picButton.imageView.image);
            }
        }else{
            
            [self.bgImageView setImage:image];
            
            
        }
        
        
        
    }];
    
}

-(void)clickDis:(DiseaserTopView *)diseaser addClickButton:(UIButton *)button{
    self.imagePickerVc = [[UIImagePickerController alloc] init];
    
    //资源类型为图片库
    
    self.imagePickerVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePickerVc.delegate = self;
    self.imagePickerVc.allowsEditing = YES;
    self.open = NO;
    
    [self presentViewController:self.imagePickerVc animated:YES completion:nil];
    
}



@end
